/*
 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 1. The origin of this software must not be misrepresented; you must not
 * claim that you wrote the original software. If you use this software
 * in a product, an acknowledgment in the product documentation would be
 * appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 * misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */

package org.box2dflash.dynamics.joints {
	import org.box2dflash.dynamics.joints.Joint;
	import org.box2dflash.dynamics.Body;
	import org.box2dflash.common.math.Mat22;
	import org.box2dflash.common.math.*;
	import org.box2dflash.common.*;
	import org.box2dflash.dynamics.*;
	use namespace Internal;

	/// A revolute joint constrains to bodies to share a common point while they
	/// are free to rotate about the point. The relative rotation about the shared
	/// point is the joint angle. You can limit the relative rotation with
	/// a joint limit that specifies a lower and upper angle. You can use a motor
	/// to drive the relative rotation about the shared point. A maximum motor torque
	/// is provided so that infinite forces are not generated.

	// Point-to-point constraint
	// C = p2 - p1
	// Cdot = v2 - v1
	//      = v2 + cross(w2, r2) - v1 - cross(w1, r1)
	// J = [-I -r1_skew I r2_skew ]
	// Identity used:
	// w k % (rx i + ry j) = w * (-ry i + rx j)

	// Motor constraint
	// Cdot = w2 - w1
	// J = [0 0 -1 0 0 1]
	// K = invI1 + invI2
	public class RevoluteJoint extends Joint {
		public override function get anchor1():Vec2 {
			return m_body1.worldPoint(m_localAnchor1);
		}

		public override function get anchor2():Vec2 {
			return m_body2.worldPoint(m_localAnchor2);
		}

		public override function get reactionForce():Vec2 {
			return m_pivotForce;
		}

		public override function get reactionTorque():Number {
			return m_limitForce;
		}

		/// Get the current joint angle in radians.
		public function get jointAngle():Number {
			//Body* b1 = m_body1;
			//Body*  = m_body2;
			return m_body2.m_sweep.a - m_body1.m_sweep.a - m_referenceAngle;
		}

		/// Get the current joint angle speed in radians per second.
		public function get jointSpeed():Number {
			//Body* b1 = m_body1;
			//Body*  = m_body2;
			return m_body2.m_angularVelocity - m_body1.m_angularVelocity;
		}

		/// Is the joint limit enabled?
		public function get isLimitEnabled():Boolean {
			return m_enableLimit;
		}

		/// Enable/disable the joint limit.
		public function enableLimit(flag:Boolean):void {
			m_enableLimit = flag;
		}

		/// Get the lower joint limit in radians.
		public function get lowerLimit():Number {
			return m_lowerAngle;
		}

		/// Get the upper joint limit in radians.
		public function get upperLimit():Number {
			return m_upperAngle;
		}

		/// Set the joint limits in radians.
		public function limits(lower:Number, upper:Number):void {
			//Settings.Assert(lower <= upper);
			m_lowerAngle = lower;
			m_upperAngle = upper;
		}

		/// Is the joint motor enabled?
		public function get isMotorEnabled():Boolean {
			return m_enableMotor;
		}

		/// Enable/disable the joint motor.
		public function set enableMotor(flag:Boolean):void {
			m_enableMotor = flag;
		}

		/// Set the motor speed in radians per second.
		public function set motorSpeed(speed:Number):void {
			m_motorSpeed = speed;
		}

		/// Get the motor speed in radians per second.
		public function get motorSpeed():Number {
			return m_motorSpeed;
		}

		/// Set the maximum motor torque, usually in N-m.
		public function set maxMotorTorque(torque:Number):void {
			m_maxMotorTorque = torque;
		}

		/// Get the current motor torque, usually in N-m.
		public function get motorTorque():Number {
			return m_motorForce;
		}

		//--------------- Internals Below -------------------
		public function RevoluteJoint(def:RevoluteJointDef) {
			super(def);
		
			//m_localAnchor1 = def->localAnchor1;
			m_localAnchor1.v(def.localAnchor1);
			//m_localAnchor2 = def->localAnchor2;
			m_localAnchor2.v(def.localAnchor2);
		
			m_referenceAngle = def.referenceAngle;
		
			m_pivotForce = new Vec2(0.0, 0.0);
			m_motorForce = 0.0;
			m_limitForce = 0.0;
			m_limitPositionImpulse = 0.0;
		
			m_lowerAngle = def.lowerAngle;
			m_upperAngle = def.upperAngle;
			m_maxMotorTorque = def.maxMotorTorque;
			m_motorSpeed = def.motorSpeed;
			m_enableLimit = def.enableLimit;
			m_enableMotor = def.enableMotor;
		}

		// internal vars
		private var K:Mat22 = new Mat22();
		private var K1:Mat22 = new Mat22();
		private var K2:Mat22 = new Mat22();
		private var K3:Mat22 = new Mat22();

		Internal override function initVelocityConstraints(step:TimeStep):void {
			var b1:Body = m_body1;
			var b2:Body = m_body2;
		
			var tMat:Mat22;
			var tX:Number;
		
			// Compute the effective mass matrix.
			//Vec2 r1 = Mul(b1->m_xf.R, m_localAnchor1 - b1->GetLocalCenter());
			tMat = b1.m_xf.R;
			var r1X:Number = m_localAnchor1.x - b1.m_sweep.localCenter.x;
			var r1Y:Number = m_localAnchor1.y - b1.m_sweep.localCenter.y;
			tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
			r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
			r1X = tX;
			//Vec2 r2 = Mul(->m_xf.R, m_localAnchor2 - ->GetLocalCenter());
			tMat = b2.m_xf.R;
			var r2X:Number = m_localAnchor2.x - b2.m_sweep.localCenter.x;
			var r2Y:Number = m_localAnchor2.y - b2.m_sweep.localCenter.y;
			tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
			r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
			r2X = tX;
		
			// K    = [(1/m1 + 1/m2) * eye(2) - skew(r1) * invI1 * skew(r1) - skew(r2) * invI2 * skew(r2)]
			//      = [1/m1+1/m2     0    ] + invI1 * [r1.y*r1.y -r1.x*r1.y] + invI2 * [r1.y*r1.y -r1.x*r1.y]
			//        [    0     1/m1+1/m2]           [-r1.x*r1.y r1.x*r1.x]           [-r1.x*r1.y r1.x*r1.x]
			var invMass1:Number = b1.m_invMass;
			var invMass2:Number = b2.m_invMass;
			var invI1:Number = b1.m_invI;
			var invI2:Number = b2.m_invI;
		
			//var K1:Mat22 = new Mat22();
			K1.col1.x = invMass1 + invMass2;	
			K1.col2.x = 0.0;
			K1.col1.y = 0.0;					
			K1.col2.y = invMass1 + invMass2;
		
			//var K2:Mat22 = new Mat22();
			K2.col1.x = invI1 * r1Y * r1Y;	
			K2.col2.x = -invI1 * r1X * r1Y;
			K2.col1.y = -invI1 * r1X * r1Y;	
			K2.col2.y = invI1 * r1X * r1X;
		
			//var K3:Mat22 = new Mat22();
			K3.col1.x = invI2 * r2Y * r2Y;	
			K3.col2.x = -invI2 * r2X * r2Y;
			K3.col1.y = -invI2 * r2X * r2Y;	
			K3.col2.y = invI2 * r2X * r2X;
		
			//var K:Mat22 = Box2dMath.AddMM(Box2dMath.AddMM(K1, K2), K3);
			K.m(K1);
			K.addM(K2);
			K.addM(K3);
		
			//m_pivotMass = K.invert();
			K.invert(m_pivotMass);
		
			m_motorMass = 1.0 / (invI1 + invI2);
		
			if (m_enableMotor == false) {
				m_motorForce = 0.0;
			}
		
			if (m_enableLimit) {
				//float32 jointAngle = ->m_sweep.a - b1->m_sweep.a - m_referenceAngle;
				var jointAngle:Number = b2.m_sweep.a - b1.m_sweep.a - m_referenceAngle;
				if (Box2dMath.abs(m_upperAngle - m_lowerAngle) < 2.0 * Settings._angularSlop) {
					m_limitState = e_equalLimits;
				}
			else if (jointAngle <= m_lowerAngle) {
					if (m_limitState != e_atLowerLimit) {
						m_limitForce = 0.0;
					}
					m_limitState = e_atLowerLimit;
				}
			else if (jointAngle >= m_upperAngle) {
					if (m_limitState != e_atUpperLimit) {
						m_limitForce = 0.0;
					}
					m_limitState = e_atUpperLimit;
				}
			else {
					m_limitState = e_inactiveLimit;
					m_limitForce = 0.0;
				}
			}
		else {
				m_limitForce = 0.0;
			}
		
			// Warm starting.
			if (step.warmStarting) {
				//b1->m_linearVelocity -= step.dt * invMass1 * m_pivotForce;
				b1.m_linearVelocity.x -= step.dt * invMass1 * m_pivotForce.x;
				b1.m_linearVelocity.y -= step.dt * invMass1 * m_pivotForce.y;
				//b1->m_angularVelocity -= step.dt * invI1 * (Cross(r1, m_pivotForce) + m_motorForce + m_limitForce);
				b1.m_angularVelocity -= step.dt * invI1 * ((r1X * m_pivotForce.y - r1Y * m_pivotForce.x) + m_motorForce + m_limitForce);
			
				//->m_linearVelocity += step.dt * invMass2 * m_pivotForce;
				b2.m_linearVelocity.x += step.dt * invMass2 * m_pivotForce.x;
				b2.m_linearVelocity.y += step.dt * invMass2 * m_pivotForce.y;
				//->m_angularVelocity += step.dt * invI2 * (Cross(r2, m_pivotForce) + m_motorForce + m_limitForce);
				b2.m_angularVelocity += step.dt * invI2 * ((r2X * m_pivotForce.y - r2Y * m_pivotForce.x) + m_motorForce + m_limitForce);
			}
		else {
				m_pivotForce.zero();
				m_motorForce = 0.0;
				m_limitForce = 0.0;
			}
		
			m_limitPositionImpulse = 0.0;
		}

		Internal override function solveVelocityConstraints(step:TimeStep):void {
			var b1:Body = m_body1;
			var b2:Body = m_body2;
		
			var tMat:Mat22;
			var tX:Number;
		
			//Vec2 r1 = Mul(b1->m_xf.R, m_localAnchor1 - b1->GetLocalCenter());
			tMat = b1.m_xf.R;
			var r1X:Number = m_localAnchor1.x - b1.m_sweep.localCenter.x;
			var r1Y:Number = m_localAnchor1.y - b1.m_sweep.localCenter.y;
			tX = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
			r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
			r1X = tX;
			//Vec2 r2 = Mul(->m_xf.R, m_localAnchor2 - ->GetLocalCenter());
			tMat = b2.m_xf.R;
			var r2X:Number = m_localAnchor2.x - b2.m_sweep.localCenter.x;
			var r2Y:Number = m_localAnchor2.y - b2.m_sweep.localCenter.y;
			tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
			r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
			r2X = tX;
		
			var oldLimitForce:Number;
		
			// Solve point-to-point constraint
			//Vec2 pivotCdot = b2.m_linearVelocity + Cross(b2.m_angularVelocity, r2) - b1.m_linearVelocity - Cross(b1.m_angularVelocity, r1);
			var pivotCdotX:Number = b2.m_linearVelocity.x + (-b2.m_angularVelocity * r2Y) - b1.m_linearVelocity.x - (-b1.m_angularVelocity * r1Y);
			var pivotCdotY:Number = b2.m_linearVelocity.y + (b2.m_angularVelocity * r2X) - b1.m_linearVelocity.y - (b1.m_angularVelocity * r1X);
		
			//Vec2 pivotForce = -step.inv_dt * Mul(m_pivotMass, pivotCdot);
			var pivotForceX:Number = -step.inv_dt * (m_pivotMass.col1.x * pivotCdotX + m_pivotMass.col2.x * pivotCdotY);
			var pivotForceY:Number = -step.inv_dt * (m_pivotMass.col1.y * pivotCdotX + m_pivotMass.col2.y * pivotCdotY);
			m_pivotForce.x += pivotForceX;
			m_pivotForce.y += pivotForceY;
		
			//Vec2 P = step.dt * pivotForce;
			var PX:Number = step.dt * pivotForceX;
			var PY:Number = step.dt * pivotForceY;
		
			//b1->m_linearVelocity -= b1->m_invMass * P;
			b1.m_linearVelocity.x -= b1.m_invMass * PX;
			b1.m_linearVelocity.y -= b1.m_invMass * PY;
			//b1->m_angularVelocity -= b1->m_invI * Cross(r1, P);
			b1.m_angularVelocity -= b1.m_invI * (r1X * PY - r1Y * PX);
		
			//->m_linearVelocity += ->m_invMass * P;
			b2.m_linearVelocity.x += b2.m_invMass * PX;
			b2.m_linearVelocity.y += b2.m_invMass * PY;
			//->m_angularVelocity += ->m_invI * Cross(r2, P);
			b2.m_angularVelocity += b2.m_invI * (r2X * PY - r2Y * PX);
		
			if (m_enableMotor && m_limitState != e_equalLimits) {
				var motorCdot:Number = b2.m_angularVelocity - b1.m_angularVelocity - m_motorSpeed;
				var motorForce:Number = -step.inv_dt * m_motorMass * motorCdot;
				var oldMotorForce:Number = m_motorForce;
				m_motorForce = Box2dMath.clamp(m_motorForce + motorForce, -m_maxMotorTorque, m_maxMotorTorque);
				motorForce = m_motorForce - oldMotorForce;
			
				b1.m_angularVelocity -= b1.m_invI * step.dt * motorForce;
				b2.m_angularVelocity += b2.m_invI * step.dt * motorForce;
			}
		
			if (m_enableLimit && m_limitState != e_inactiveLimit) {
				var limitCdot:Number = b2.m_angularVelocity - b1.m_angularVelocity;
				var limitForce:Number = -step.inv_dt * m_motorMass * limitCdot;
			
				if (m_limitState == e_equalLimits) {
					m_limitForce += limitForce;
				}
			else if (m_limitState == e_atLowerLimit) {
					oldLimitForce = m_limitForce;
					m_limitForce = Box2dMath.max(m_limitForce + limitForce, 0.0);
					limitForce = m_limitForce - oldLimitForce;
				}
			else if (m_limitState == e_atUpperLimit) {
					oldLimitForce = m_limitForce;
					m_limitForce = Box2dMath.min(m_limitForce + limitForce, 0.0);
					limitForce = m_limitForce - oldLimitForce;
				}
			
				b1.m_angularVelocity -= b1.m_invI * step.dt * limitForce;
				b2.m_angularVelocity += b2.m_invI * step.dt * limitForce;
			}
		}

		public static var tImpulse:Vec2 = new Vec2();

		Internal override function solvePositionConstraints():Boolean {
		
			var oldLimitImpulse:Number;
			var limitC:Number;
		
			var b1:Body = m_body1;
			var b2:Body = m_body2;
		
			var positionError:Number = 0.0;
		
			var tMat:Mat22;
		
			// Solve point-to-point position error.
			//Vec2 r1 = Mul(b1->m_xf.R, m_localAnchor1 - b1->GetLocalCenter());
			tMat = b1.m_xf.R;
			var r1X:Number = m_localAnchor1.x - b1.m_sweep.localCenter.x;
			var r1Y:Number = m_localAnchor1.y - b1.m_sweep.localCenter.y;
			var tX:Number = (tMat.col1.x * r1X + tMat.col2.x * r1Y);
			r1Y = (tMat.col1.y * r1X + tMat.col2.y * r1Y);
			r1X = tX;
			//Vec2 r2 = Mul(->m_xf.R, m_localAnchor2 - ->GetLocalCenter());
			tMat = b2.m_xf.R;
			var r2X:Number = m_localAnchor2.x - b2.m_sweep.localCenter.x;
			var r2Y:Number = m_localAnchor2.y - b2.m_sweep.localCenter.y;
			tX = (tMat.col1.x * r2X + tMat.col2.x * r2Y);
			r2Y = (tMat.col1.y * r2X + tMat.col2.y * r2Y);
			r2X = tX;
		
			//Vec2 p1 = b1->m_sweep.c + r1;
			var p1X:Number = b1.m_sweep.c.x + r1X;
			var p1Y:Number = b1.m_sweep.c.y + r1Y;
			//Vec2 p2 = ->m_sweep.c + r2;
			var p2X:Number = b2.m_sweep.c.x + r2X;
			var p2Y:Number = b2.m_sweep.c.y + r2Y;
		
			//Vec2 ptpC = p2 - p1;
			var ptpCX:Number = p2X - p1X;
			var ptpCY:Number = p2Y - p1Y;
		
			//float32 positionError = ptpC.length;
			positionError = Math.sqrt(ptpCX * ptpCX + ptpCY * ptpCY);
		
			// Prevent overly large corrections.
			//Vec2 dpMax(_maxLinearCorrection, _maxLinearCorrection);
			//ptpC = Clamp(ptpC, -dpMax, dpMax);
		
			//float32 invMass1 = b1->m_invMass, invMass2 = ->m_invMass;
			var invMass1:Number = b1.m_invMass;
			var invMass2:Number = b2.m_invMass;
			//float32 invI1 = b1->m_invI, invI2 = ->m_invI;
			var invI1:Number = b1.m_invI;
			var invI2:Number = b2.m_invI;
		
			//Mat22 K1;
			K1.col1.x = invMass1 + invMass2;	
			K1.col2.x = 0.0;
			K1.col1.y = 0.0;					
			K1.col2.y = invMass1 + invMass2;
		
			//Mat22 K2;
			K2.col1.x = invI1 * r1Y * r1Y;	
			K2.col2.x = -invI1 * r1X * r1Y;
			K2.col1.y = -invI1 * r1X * r1Y;	
			K2.col2.y = invI1 * r1X * r1X;
		
			//Mat22 K3;
			K3.col1.x = invI2 * r2Y * r2Y;		
			K3.col2.x = -invI2 * r2X * r2Y;
			K3.col1.y = -invI2 * r2X * r2Y;		
			K3.col2.y = invI2 * r2X * r2X;
		
			//Mat22 K = K1 + K2 + K3;
			K.m(K1);
			K.addM(K2);
			K.addM(K3);
			//Vec2 impulse = K.Solve(-ptpC);
			K.solve(tImpulse, -ptpCX, -ptpCY);
			var impulseX:Number = tImpulse.x;
			var impulseY:Number = tImpulse.y;
		
			//b1.m_sweep.c -= b1.m_invMass * impulse;
			b1.m_sweep.c.x -= b1.m_invMass * impulseX;
			b1.m_sweep.c.y -= b1.m_invMass * impulseY;
			//b1.m_sweep.a -= b1.m_invI * Cross(r1, impulse);
			b1.m_sweep.a -= b1.m_invI * (r1X * impulseY - r1Y * impulseX);
		
			//.m_sweep.c += b2.m_invMass * impulse;
			b2.m_sweep.c.x += b2.m_invMass * impulseX;
			b2.m_sweep.c.y += b2.m_invMass * impulseY;
			//.m_sweep.a += b2.m_invI * Cross(r2, impulse);
			b2.m_sweep.a += b2.m_invI * (r2X * impulseY - r2Y * impulseX);
		
			b1.synchronizeTransform();
			b2.synchronizeTransform();
		
		
			// Handle limits.
			var angularError:Number = 0.0;
		
			if (m_enableLimit && m_limitState != e_inactiveLimit) {
				var angle:Number = b2.m_sweep.a - b1.m_sweep.a - m_referenceAngle;
				var limitImpulse:Number = 0.0;
			
				if (m_limitState == e_equalLimits) {
					// Prevent large angular corrections
					limitC = Box2dMath.clamp(angle, -Settings._maxAngularCorrection, Settings._maxAngularCorrection);
					limitImpulse = -m_motorMass * limitC;
					angularError = Box2dMath.abs(limitC);
				}
			else if (m_limitState == e_atLowerLimit) {
					limitC = angle - m_lowerAngle;
					angularError = Box2dMath.max(0.0, -limitC);
				
					// Prevent large angular corrections and allow some slop.
					limitC = Box2dMath.clamp(limitC + Settings._angularSlop, -Settings._maxAngularCorrection, 0.0);
					limitImpulse = -m_motorMass * limitC;
					oldLimitImpulse = m_limitPositionImpulse;
					m_limitPositionImpulse = Box2dMath.max(m_limitPositionImpulse + limitImpulse, 0.0);
					limitImpulse = m_limitPositionImpulse - oldLimitImpulse;
				}
			else if (m_limitState == e_atUpperLimit) {
					limitC = angle - m_upperAngle;
					angularError = Box2dMath.max(0.0, limitC);
				
					// Prevent large angular corrections and allow some slop.
					limitC = Box2dMath.clamp(limitC - Settings._angularSlop, 0.0, Settings._maxAngularCorrection);
					limitImpulse = -m_motorMass * limitC;
					oldLimitImpulse = m_limitPositionImpulse;
					m_limitPositionImpulse = Box2dMath.min(m_limitPositionImpulse + limitImpulse, 0.0);
					limitImpulse = m_limitPositionImpulse - oldLimitImpulse;
				}
			
				b1.m_sweep.a -= b1.m_invI * limitImpulse;
				b2.m_sweep.a += b2.m_invI * limitImpulse;
			
				b1.synchronizeTransform();
				b2.synchronizeTransform();
			}
		
			return positionError <= Settings._linearSlop && angularError <= Settings._angularSlop;
		}

		public var m_localAnchor1:Vec2 = new Vec2(); 
		// relative
		public var m_localAnchor2:Vec2 = new Vec2();
		public var m_pivotForce:Vec2 = new Vec2();
		public var m_motorForce:Number;
		public var m_limitForce:Number;
		public var m_limitPositionImpulse:Number;
		public var m_pivotMass:Mat22 = new Mat22();		
		// effective mass for point-to-point constraint.
		public var m_motorMass:Number;	
		// effective mass for motor/limit angular constraint.
		public var m_enableMotor:Boolean;
		public var m_maxMotorTorque:Number;
		public var m_motorSpeed:Number;
		public var m_enableLimit:Boolean;
		public var m_referenceAngle:Number;
		public var m_lowerAngle:Number;
		public var m_upperAngle:Number;
		public var m_limitState:int;
	}
}
